#include "dataproc.h"

/*******************************************************************************
* @brief  quene manage
*******************************************************************************/


#define dat_copy(r, d, len) \
{ 							\
	uint16_t cnt; 			\
	uint32_t * src, *des;  	\
	cnt = len;			   	\
	src = (uint32_t *)(r); 	\
	des = (uint32_t *)(d); 	\
	while (cnt--)          	\
		*des++ = *src++;   	\
} 							\

/*******************************************************************************
* @brief  quene fill
* \param[in] quene : quene
* \param[in] dat : fill dat
* \retval: ok
*******************************************************************************/
uint8_t com_quene_fill(com_quene_type * quene, void *dat)
{
	uint16_t next;

	next = quene->next_fill+ 1;
	if (next >= quene->element_amount)
	{
		next = 0;
	}

	if (next != quene->next_get)
	{
		dat_copy(dat,((void *)((uint32_t)quene->buff + quene->next_fill * quene->element_size)),quene->element_size >> 2);

		quene->next_fill = next;

		return 1;
	}

	return 0;
}

/*******************************************************************************
* @brief  quene dat get
* \param[in] quene : quene
* \param[in] dat : dat get
* \retval: ok
*******************************************************************************/
uint8_t com_quene_get(com_quene_type * quene, void *dat)
{
	uint16_t site_now;

	site_now = quene->next_get;
	
	if (quene->next_fill != site_now)
	{
		dat_copy(((void *)((uint32_t)quene->buff + site_now * quene->element_size)),dat,quene->element_size >> 2);

		site_now++;
		if (site_now >= quene->element_amount)
		{
			site_now = 0;
		}

		quene->next_get = site_now;

		return 1;
	}

	return 0;
}

/*******************************************************************************
* @brief  quene used
* \param[in] quene : quene
* \retval: amount
*******************************************************************************/
uint16_t com_quene_used_amount(com_quene_type * quene)
{
	uint16_t amount;

	if (quene->next_fill < quene->next_get)
		amount = quene->element_amount + quene->next_fill - quene->next_get;
	else
		amount = quene->next_fill - quene->next_get;

	return amount;
}

/*******************************************************************************
* @brief  quene remain
* \param[in] quene : quene
* \retval: amount
*******************************************************************************/
uint16_t com_quene_remain_amount(com_quene_type * quene)
{
	uint16_t amount;

	amount = quene->element_amount - com_quene_used_amount(quene);

	return amount;
}
